package com.ruoyi.sbxm.bjgh.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.sbxm.bjgh.domain.Bjgh;
import com.ruoyi.sbxm.bjgh.domain.BjghFj;
import com.ruoyi.sbxm.bjgh.service.IBjghService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.List;

/**
 * 报奖规划Controller
 *
 * @author huacai
 * @date 2024-11-12
 */
@RestController
@RequestMapping("/sbxm/bjgh")
public class BjghController extends BaseController {
    @Resource
    private IBjghService bjghService;

    /**
     * 查询报奖规划列表
     */
    @PreAuthorize("@ss.hasPermi('sbxm:bjgh:list')")
    @GetMapping("/list")
    public TableDataInfo list(Bjgh bjgh) {
        startPage();
        List<Bjgh> list = bjghService.selectBjghList(bjgh);
        return getDataTable(list);
    }

    /**
     * 导出报奖规划列表
     */
    @PreAuthorize("@ss.hasPermi('sbxm:bjgh:export')")
    @Log(title = "报奖规划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Bjgh bjgh) {
        List<Bjgh> list = bjghService.selectBjghList(bjgh);
        ExcelUtil<Bjgh> util = new ExcelUtil<Bjgh>(Bjgh.class);
        util.exportExcel(response, list, "报奖规划数据");
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Bjgh> util = new ExcelUtil<Bjgh>(Bjgh.class);
        util.importTemplateExcel(response, "报奖规划数据");
    }

    /**
     * 导入数据
     */
    @Log(title = "报奖规划", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('sbxm:bjgh:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Bjgh> util = new ExcelUtil<Bjgh>(Bjgh.class);
        InputStream inputStream = file.getInputStream();
        List<Bjgh> list = util.importExcel(inputStream);
        inputStream.close();
        int count = bjghService.batchInsertBjgh(list);
        return AjaxResult.success("导入成功" + count + "条信息！");
    }

    /**
     * 获取报奖规划详细信息
     */
    @PreAuthorize("@ss.hasPermi('sbxm:bjgh:query')")
    @GetMapping(value = "/{bjghId}")
    public AjaxResult getInfo(@PathVariable("bjghId") String bjghId) {
        return success(bjghService.selectBjghByBjghId(bjghId));
    }

    /**
     * 新增报奖规划
     */
    @PreAuthorize("@ss.hasPermi('sbxm:bjgh:add')")
    @Log(title = "报奖规划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Bjgh bjgh) {
        bjgh.setUserId(Math.toIntExact(getUserId())); //用户ID
        bjgh.setDeptId(Math.toIntExact(getDeptId())); //部门ID
        bjgh.setCreateBy(getUsername()); //创建人
        bjgh.setIdWhere("存在");
        return toAjax(bjghService.insertBjgh(bjgh));
    }

    /**
     * 修改报奖规划
     */
    @PreAuthorize("@ss.hasPermi('sbxm:bjgh:edit')")
    @Log(title = "报奖规划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Bjgh bjgh) {
        return toAjax(bjghService.updateBjgh(bjgh));
    }

    /**
     * 删除报奖规划
     */
    @PreAuthorize("@ss.hasPermi('sbxm:bjgh:remove')")
    @Log(title = "报奖规划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bjghIds}")
    public AjaxResult remove(@PathVariable String[] bjghIds) {
        return toAjax(bjghService.deleteBjghByBjghIds(bjghIds));
    }

    /**
     * 获得报奖规划主键ID (UUID)
     */
    @GetMapping("/getBjGhId")
    public AjaxResult getBjGhId() {
        String getBjGhId = IdUtils.fastSimpleUUID();
        return success(getBjGhId);
    }

    /**
     * 新增附件
     */
    @PostMapping("/add/File/By/subId")
    public AjaxResult insertBjghFjByBjghId(@RequestBody BjghFj bjghFj) {
        return toAjax(bjghService.insertBjghFjByBjghId(bjghFj));
    }

    /**
     * 根据ID查询附件列表
     */
    @GetMapping("/get/FileList/By/subId")
    public AjaxResult selectBjghFjList(String sId) {
        BjghFj bjghFj = new BjghFj();
        bjghFj.setBjghId(sId);
        return AjaxResult.success(bjghService.selectBjghFjList(bjghFj));
    }

    /**
     * 删除报奖规划附件
     */
    @Log(title = "报奖规划附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/fj/{fjIds}")
    public AjaxResult deleteBjghFjByFjIds(@PathVariable Integer[] fjIds) {
        return toAjax(bjghService.deleteBjghFjByFjIds(fjIds));
    }

}
